﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;
using QuanLiThuVien.Model;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for QuyDinh.xaml
    /// </summary>
    public partial class QuyDinh : Page
    {
        private ShowInfo m_showInfo = new ShowInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private bool m_bInsertData = false;
        
        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        //XuLyThem m_XuLyThem = new XuLyThem();

        public QuyDinh()
        {
            InitializeComponent();
            ShowQuyDinh();
            m_showInfo.ShowListTheLoai(dataQDTheLoai);
        }

        public void ShowQuyDinh()
        {
            txtTuoiToiDa.Text = serv.SelectGiaTriThamSo("TuoiToiDa").ToString();
            txtTuoiToiThieu.Text = serv.SelectGiaTriThamSo("TuoiToiThieu").ToString();
            txtThoiHanThe.Text = serv.SelectGiaTriThamSo("ThoiHanThe").ToString();
            txtKhoangCachNXB.Text = serv.SelectGiaTriThamSo("KhoangCachNamXB").ToString();
            txtSoNgayMuonTD.Text = serv.SelectGiaTriThamSo("NgayMuonToiDa").ToString();
            txtSoSachMuonTD.Text = serv.SelectGiaTriThamSo("SachMuonToiDa").ToString();
            txtTienPhat.Text = serv.SelectGiaTriThamSo("TienPhat").ToString();
        }

        public void UpdateQuyDinh()
        {
            serv.UpdateGiaTriThamSo("TuoiToiDa", int.Parse(txtTuoiToiDa.Text));
            serv.UpdateGiaTriThamSo("TuoiToiThieu", int.Parse(txtTuoiToiThieu.Text));
            serv.UpdateGiaTriThamSo("ThoiHanThe", int.Parse(txtThoiHanThe.Text));
            serv.UpdateGiaTriThamSo("KhoangCachNamXB", int.Parse(txtKhoangCachNXB.Text));
            serv.UpdateGiaTriThamSo("SoNgayMuonToiDa", int.Parse(txtSoNgayMuonTD.Text));
            serv.UpdateGiaTriThamSo("SachMuonToiDa", int.Parse(txtSoSachMuonTD.Text));
            serv.UpdateGiaTriThamSo("TienPhat", int.Parse(txtTienPhat.Text));
        }

        private void bntChapNhan_Click(object sender, RoutedEventArgs e)
        {

            if (!KiemTraTextBox(txtTuoiToiThieu.Text)||
                !KiemTraTextBox(txtTuoiToiDa.Text)||
                !KiemTraTextBox(txtThoiHanThe.Text)||
                !KiemTraTextBox(txtTienPhat.Text)||
                !KiemTraTextBox(txtKhoangCachNXB.Text)||
                !KiemTraTextBox(txtSoNgayMuonTD.Text)||
                !KiemTraTextBox(txtSoSachMuonTD.Text))
            {
                MessageBox.Show("Chưa nhập đầy đủ dữ liệu", "Thông báo");
            }
            else
            {

                if (txtTuoiToiThieu.Text != "" && txtTuoiToiDa.Text != "")
                {
                    if (int.Parse(txtTuoiToiDa.Text) <= int.Parse(txtTuoiToiThieu.Text))
                    {
                        MessageBox.Show("Tuổi tối đa phải lớn hơn tuổi tối thiểu", "Thông báo");
                        txtTuoiToiDa.Text = "";
                        txtTuoiToiDa.Focus();
                    }

                    else
                    {
                        UpdateQuyDinh();
                        MessageBox.Show("Thành Công");
                    }
                }
            }
        }

        private void txtTuoiToiThieu_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;

            if (txtTuoiToiThieu.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtTuoiToiThieu.Text = "";
            }
            check = KiemTra(txtTuoiToiThieu.Text);

            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtTuoiToiThieu.Text = "";
            }
            //m_XuLyThem.isNumber(txtTuoiToiThieu);
        }

        private void txtThoiHanThe_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;
            if (txtThoiHanThe.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtThoiHanThe.Text = "";
            }
            check = KiemTra(txtThoiHanThe.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtThoiHanThe.Text = "";
            }
           // m_XuLyThem.isNumber(txtThoiHanThe);
        }

        private void txtKhoangCachNXB_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;
            if (txtKhoangCachNXB.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtKhoangCachNXB.Text = "";
            }
            check = KiemTra(txtKhoangCachNXB.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtKhoangCachNXB.Text = "";
            }
            //m_XuLyThem.isNumber(txtKhoangCachNXB);
        }

        private void txtSoSachMuonTD_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;
            if (txtSoSachMuonTD.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtSoSachMuonTD.Text = "";
            }
            check = KiemTra(txtSoSachMuonTD.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtSoSachMuonTD.Text = "";
            }
           // m_XuLyThem.isNumber(txtSoSachMuonTD);
        }

        private void txtTuoiToiDa_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;
            if (txtTuoiToiDa.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtTuoiToiDa.Text = "";
            }
            check = KiemTra(txtTuoiToiDa.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtTuoiToiDa.Text = "";
            }
            //m_XuLyThem.isNumber(txtTuoiToiDa);
        }

        private void txtSoNgayMuonTD_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;
            if (txtSoNgayMuonTD.Text == "0")
            {
                MessageBox.Show("Lỗi");
                txtSoNgayMuonTD.Text = "";
            }
            check = KiemTra(txtSoNgayMuonTD.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtSoNgayMuonTD.Text = "";
            }
            //m_XuLyThem.isNumber(txtSoNgayMuonTD);
        }

        private void txtTienPhat_TextChanged(object sender, TextChangedEventArgs e)
        {
            bool check = true;

            check = KiemTra(txtTienPhat.Text);
            if (!check)
            {
                MessageBox.Show("Lỗi");
                txtTienPhat.Text = "";
            }
            //m_XuLyThem.isNumber(txtTienPhat);
        }

        public bool KiemTraTextBox(string str)
        {
            bool check = true;
            if (str=="")
            {
                check = false;
                return check;
            }
            return check;
        }

        public bool KiemTra(string str)
        {
            bool check = true;

            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '0' ||
                    str[i] == '1' ||
                    str[i] == '2' ||
                    str[i] == '3' ||
                    str[i] == '4' ||
                    str[i] == '5' ||
                    str[i] == '6' ||
                    str[i] == '7' ||
                    str[i] == '8' ||
                    str[i] == '9')
                {
                    check = true;
                }
                else
                {
                    check = false;
                    return check;
                }
            }
            return check;
        }

        private void bntChapNhanTL_Click(object sender, RoutedEventArgs e)
        {
            //if(serv.SelectTheLoai_TenTheLoai())
            if (txtTheLoai.Text == "")
                MessageBox.Show("Hãy nhập tên thể loại", "Thông báo");
            else
            {
                serv.InsertTheLoai(txtTheLoai.Text);
                MessageBox.Show("Thành Công");
                m_showInfo.ShowListTheLoai(dataQDTheLoai);
                txtTheLoai.Text = "";
            }
        }

        private void dataQDTheLoai_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtTheLoai.Text = "";
            bntChapNhanTL.IsEnabled = false;
            if (!m_bInsertData)
            {
                var item = dataQDTheLoai.SelectedItem as TheLoai;
                txtTheLoai.Text = item.TenTheLoai.ToString();
            }
            else
            {
                m_bInsertData = false;
                m_showInfo.ShowListTheLoai(dataQDTheLoai);
            }
        }

        private void bntSuaTL_Click(object sender, RoutedEventArgs e)
        {
            m_bInsertData = true;
            var item = dataQDTheLoai.SelectedItem as 
                TheLoai;
            string m_sMaTheLoai = item.MaTheLoai.ToString();
            m_updateInfo.UpdateTheLoai(m_sMaTheLoai, txtTheLoai.Text);
            if (!m_bInsertData)
                m_bInsertData = true;
            txtTheLoai.Text = "";
            dataQDTheLoai.ItemsSource = null;
            m_showInfo.ShowListTheLoai(dataQDTheLoai);
        }

        private void bntRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bInsertData)
                m_bInsertData = true;
            txtTheLoai.Text = "";
            dataQDTheLoai.ItemsSource = null;
            bntChapNhanTL.IsEnabled = true;
            m_bInsertData = false;
            m_showInfo.ShowListTheLoai(dataQDTheLoai);
        }
    }
}
